angular.module("busOrderApp",['ui.bootstrap','httpService']).
controller("busOrderCtrl",['$scope','$http','$uibModal','$timeout','busOrderService',function($scope,$http,$modal,$timeout,busOrderService){
     $scope.addBusOrder=function(){
        $modal.open({
            templateUrl:"modifyBusOrder",
            controller:"insertBusOrderCtrl",
            scope:$scope
        });
    };
    
    $scope.busOrder={};
 //   $scope.busOrder.departuredate=new Date().format('yyyy-MM-dd');   
  
    
    busOrderService.getByCount({start:0,count:36}).then(function(result){
        $scope.busOrders=result;
    });
    $scope.updateBusOrder=function(){
        if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        $modal.open({
            templateUrl:"modifyBusOrder",
            scope:$scope,
            controller:"updateBusOrderCtrl"
        });
    };

    $scope.refreshBusOrder=function(){
        busOrderService.getByCount({start:0,count:36}).then(function(result){
        $scope.busOrders=result;
    });
    };
    $scope.deleteBusOrder=function(){
         if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        var tmp=window.confirm("确认是否删除");
        if (tmp!=true) {
            return;
            };
        var tmpid=$scope.busOrders[$scope.checkedIndex].id;
        busOrderService.delete({id:tmpid}).then(function(){
            alert("删除成功");
        });
    };
    $scope.checkedIndex = -1;
    $scope.selectChange=function(index){         
       if($scope.busOrders[index].checked==true){
            $scope.busOrders[index].checked=false;
          }
       else{
            $scope.busOrders[index].checked=true;
           }
        if($scope.busOrders[index].checked){
            $scope.checkedIndex = index;
            return;
        }
        for(var i = 0; i < $scope.busOrders.length; i++){
            var item = $scope.busOrders[i];
            if(item.checked != undefined && item.checked == true){
                $scope.checkedIndex = i;
                return;
            }
        }
        $scope.checkedIndex = -1;
    };
}]).
controller("insertBusOrderCtrl",['$scope','$uibModalInstance','busOrderService',function($scope,$modalInstance,busOrderService){
    $scope.oritentions=[{id:0,name:"成都方向"},{id:1,name:"基地方向"}];
    $scope.close=function(){
        $modalInstance.close();
    };
    $scope.isEabled=false;
    $scope.busOrder={};
    $scope.busOrder.departuredate=new Date().format('yyyy-MM-dd');
    $scope.busOrder.validitydate=new Date().format('yyyy-MM-dd') + " 18:00:00"; 
    $scope.busOrder.departuredate=$scope.busOrder.departuredate+" 14:30:00";
    $scope.busOrder.endbusdate=new Date(new Date().getTime()).format('yyyy-MM-dd')+" 09:30:00";
    $scope.$watch("busOrder.orientation",function(value){
        var now=new Date();
        if(value==1){
            
            var month=now.getMonth()+1;
            if(month>4&&month<10){
            $scope.busOrder.departuredate=new Date().format('yyyy-MM-dd')+" 19:30:00";
            $scope.busOrder.endbusdate=new Date(now.getTime()).format('yyyy-MM-dd')+" 09:30:00";
            }
            else{
                $scope.busOrder.departuredate=new Date().format('yyyy-MM-dd')+" 19:30:00";
                $scope.busOrder.endbusdate=new Date(now.getTime()).format('yyyy-MM-dd')+" 09:30:00";
            }
        }
        else{
            $scope.busOrder.departuredate=new Date().format('yyyy-MM-dd')+" 14:30:00";
            $scope.busOrder.endbusdate=new Date(now.getTime()).format('yyyy-MM-dd')+" 09:30:00";
        }
    
    },true);
    $scope.submit=function(){
        busOrderService.insert($scope.busOrder).then(function(){                         
            $scope.$parent.refreshBusOrder();  
            alert("车次建立成功并成功建立5个班次!");       
        },function(response){
            if(response.status==1003){
                alert("班次初始化建立失败!请联系管理员");
            }
            if(response.status==1002){
                alert("建立车次失败!");
            }
        });
    };
}]).
controller("updateBusOrderCtrl",['$scope','$uibModalInstance','busOrderService',function($scope,$modalInstance,busOrderService){
    $scope.oritentions=[{id:0,name:"成都方向"},{id:1,name:"基地方向"}];
    $scope.close=function(){
        $modalInstance.close();
    };
    $scope.isEabled=true;
    $scope.busOrder=$scope.busOrders[$scope.checkedIndex];
    $scope.submit=function(){
        delete $scope.busOrder.checked;
        $scope.loading=true;
        busOrderService.update($scope.busOrder).then(function(){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="成功";
        });
    };
}]);